local love = require 'love'
local fontHandler = require('assets-handler.font-handler')

local setColor = love.graphics.setColor
local setLineWidth = love.graphics.setLineWidth
local drawRectangle = love.graphics.rectangle
local drawCircle = love.graphics.circle
local printg = love.graphics.print

local width = Width
local height = Height
local padding = Padding

local symbol = {
    heartsDiamonds = {
        name = "日", -- 红色牌视为同种花色
        color = {0.1, 0.463, 0.839, 1.0}, -- #1976D2(较深的蓝色)
        fontColor = {0.259, 0.647, 0.961, 1.0}, -- #42A5F5(较浅的蓝色)
    },
    clubsspades = {
        name = "月", -- 黑色牌视为同种花色
        color = {0.1, 0.463, 0.839, 1.0}, -- #1976D2(较深的蓝色)
        fontColor = {0.259, 0.647, 0.961, 1.0}, -- #42A5F5(较浅的蓝色)
    },
    addmaxhandcards = {
        name = "增", -- 多一张手牌
        color = {0.1, 0.463, 0.839, 1.0}, -- #1976D2(较深的蓝色)
        fontColor = {0.259, 0.647, 0.961, 1.0}, -- #42A5F5(较浅的蓝色)
    },

}

-- 天赋木色盒子
local woodColors = {
    base = {0.54, 0.27, 0.07, 1},     -- 深棕色（主体）
    highlight = {0.82, 0.41, 0.12, 1}, -- 浅棕色（边缘/高光）
    border = {0.34, 0.17, 0.03, 1},    -- 深褐色（边框）
    circleBorder = {0.25, 0.12, 0.01, 1} -- 圆形边框
}
local boxPadding = 4 -- 计算每个矩形的间距
local fontSize = 21
local boxX, boxY, boxWidth, boxHeight, circleX, textX
local rectHeight, circleRadius, rect1Y, rect2Y, rect3Y, rect4Y, rect5Y, rect6Y, circle1Y, circle2Y, circle3Y, circle4Y, circle5Y, circle6Y, text1Y, text2Y, text3Y, text4Y, text5Y, text6Y

local function init()
    boxWidth = width / 13
    boxHeight = (height - padding * 2) * 0.372
    boxX = width - padding * 2 - boxWidth
    boxY = (height - boxHeight) / 4
    circleX = boxX + boxWidth / 2

    rectHeight = boxHeight / 6
    circleRadius = (boxWidth - boxPadding) / 2
    textX = circleX - circleRadius

    rect1Y = boxY
    circle1Y = rect1Y + rectHeight / 2 - boxPadding / 2
    text1Y = circle1Y - circleRadius

    rect2Y = boxY + rectHeight
    circle2Y = rect2Y + rectHeight / 2 - boxPadding / 2
    text2Y = circle2Y - circleRadius

    rect3Y = boxY + rectHeight * 2
    circle3Y = rect3Y + rectHeight / 2 - boxPadding / 2
    text3Y = circle3Y - circleRadius

    rect4Y = boxY + rectHeight * 3
    circle4Y = rect4Y + rectHeight / 2 - boxPadding / 2
    text4Y = circle4Y - circleRadius

    rect5Y = boxY + rectHeight * 4
    circle5Y = rect5Y + rectHeight / 2 - boxPadding / 2
    text5Y = circle5Y - circleRadius

    rect6Y = boxY + rectHeight * 5
    circle6Y = rect6Y + rectHeight / 2 - boxPadding / 2
    text6Y = circle6Y - circleRadius
end

local function verticalGift(currentGift)
    -- 绘制整体边框
    setLineWidth(3)
    setColor(woodColors.border)
    drawRectangle("line", boxX, boxY, boxWidth, boxHeight)
    setLineWidth(2)

    -- 矩形 1
    setColor(woodColors.base)
    drawRectangle("fill", boxX, rect1Y, boxWidth, rectHeight - boxPadding)
    setColor(woodColors.border)
    drawRectangle("line", boxX, rect1Y, boxWidth, rectHeight - boxPadding)

    -- 矩形 2
    setColor(woodColors.base)
    drawRectangle("fill", boxX, rect2Y, boxWidth, rectHeight - boxPadding)
    setColor(woodColors.border)
    drawRectangle("line", boxX, rect2Y, boxWidth, rectHeight - boxPadding)

    -- 矩形 3
    setColor(woodColors.base)
    drawRectangle("fill", boxX, rect3Y, boxWidth, rectHeight - boxPadding)
    setColor(woodColors.border)
    drawRectangle("line", boxX, rect3Y, boxWidth, rectHeight - boxPadding)

    -- 矩形 4
    setColor(woodColors.base)
    drawRectangle("fill", boxX, rect4Y, boxWidth, rectHeight - boxPadding)
    setColor(woodColors.border)
    drawRectangle("line", boxX, rect4Y, boxWidth, rectHeight - boxPadding)

    -- 矩形 5
    setColor(woodColors.base)
    drawRectangle("fill", boxX, rect5Y, boxWidth, rectHeight - boxPadding)
    setColor(woodColors.border)
    drawRectangle("line", boxX, rect5Y, boxWidth, rectHeight - boxPadding)

    -- 矩形 6
    setColor(woodColors.base)
    drawRectangle("fill", boxX, rect6Y, boxWidth, rectHeight - boxPadding)
    setColor(woodColors.border)
    drawRectangle("line", boxX, rect6Y, boxWidth, rectHeight - boxPadding)

    setLineWidth(1)
    fontHandler.setChineseFont(fontSize)
    if #currentGift >= 6 then
        -- 圆形 6
        setColor(symbol[currentGift[6]].color)
        drawCircle("fill", circleX, circle6Y, circleRadius)
        setColor(woodColors.circleBorder)
        drawCircle("line", circleX, circle6Y, circleRadius)
        setColor(symbol[currentGift[6]].fontColor)
        printg(symbol[currentGift[6]].name, textX, text6Y)
    end

    if #currentGift >= 5 then
        -- 圆形 5
        setColor(symbol[currentGift[5]].color)
        drawCircle("fill", circleX, circle5Y, circleRadius)
        setColor(woodColors.circleBorder)
        drawCircle("line", circleX, circle5Y, circleRadius)
        setColor(symbol[currentGift[5]].fontColor)
        printg(symbol[currentGift[5]].name, textX, text5Y)
    end

    if #currentGift >= 4 then
        -- 圆形 4
        setColor(symbol[currentGift[4]].color)
        drawCircle("fill", circleX, circle4Y, circleRadius)
        setColor(woodColors.circleBorder)
        drawCircle("line", circleX, circle4Y, circleRadius)
        setColor(symbol[currentGift[4]].fontColor)
        printg(symbol[currentGift[4]].name, textX, text4Y)
    end

    if #currentGift >= 3 then
        -- 圆形 3
        setColor(symbol[currentGift[3]].color)
        drawCircle("fill", circleX, circle3Y, circleRadius)
        setColor(woodColors.circleBorder)
        drawCircle("line", circleX, circle3Y, circleRadius)
        setColor(symbol[currentGift[3]].fontColor)
        printg(symbol[currentGift[3]].name, textX, text3Y)
    end

    if #currentGift >= 2 then
        -- 圆形 2
        setColor(symbol[currentGift[2]].color)
        drawCircle("fill", circleX, circle2Y, circleRadius)
        setColor(woodColors.circleBorder)
        drawCircle("line", circleX, circle2Y, circleRadius)
        setColor(symbol[currentGift[2]].fontColor)
        printg(symbol[currentGift[2]].name, textX, text2Y)
    end

    if #currentGift >= 1 then
        -- 圆形 1
        setColor(symbol[currentGift[1]].color)
        drawCircle("fill", circleX, circle1Y, circleRadius)
        setColor(woodColors.circleBorder)
        drawCircle("line", circleX, circle1Y, circleRadius)
        setColor(symbol[currentGift[1]].fontColor)
        printg(symbol[currentGift[1]].name, textX, text1Y)
    end
end

local function drawGift(currentGift)
    if #currentGift > 0 then
        verticalGift(currentGift)
    end
end

local changeTypeProcessors = {
    heartsDiamonds = function(num)
        if num % 10 == 3 then
            return math.floor(num / 10) * 10 + 1
        end
        return num
    end,
    clubsspades = function(num)
        if num % 10 == 4 then
            return math.floor(num / 10) * 10 + 2
        end
        return num
    end
}

local function changeType(thisTurnSelectedCards, currentGift)
    local newCards = {}
    for i, value in ipairs(thisTurnSelectedCards) do
        local currentValue = value
        for _, funcName in ipairs(currentGift) do
            local func = changeTypeProcessors[funcName]
            if func then
                currentValue = func(currentValue)
            end
        end
        newCards[i] = currentValue
    end
    return newCards
end

local changeNumProcessors = {
    addmaxhandcards = function(num)
        return num + 1
    end,
}

local function changeNum(num, currentGift)
    local newNum = num
    for _, funcName in ipairs(currentGift) do
        local func = changeNumProcessors[funcName]
        if func then
            newNum = func(newNum)
        end
    end
    return newNum
end

return{
    init = init,
    drawGift = drawGift,
    changeType = changeType,
    changeNum = changeNum,
}